package edu.ccut.saturn.studio.logic.model.commands;

import java.util.Collections;
import java.util.List;

import org.eclipse.gef.commands.Command;
import org.eclipse.jface.dialogs.Dialog;

import edu.ccut.saturn.studio.logic.model.Shape;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnParameter;

public class OpenDialogCommand extends Command {
	private Dialog dialog;
	private Shape shape;

	public OpenDialogCommand(Dialog dialog, Shape shape) {
		this.dialog = dialog;
		this.shape = shape;
	}

	public void execute() {
		reorder(shape);
		dialog.open();
	}

	public boolean canExecute() {
		if (dialog != null && shape != null)
			return true;
		else
			return false;
	}

	private void reorder(Shape shape) {
		List<SaturnParameter> parameters = shape.getParameters();

		if (parameters != null) {
			Collections.sort(parameters);
			for (int i = 0; i < parameters.size(); i++) {
				shape.getParameters().get(i).setId("" + (i + 1));
			}
		}
	}
}
